<?php
/**
 * Created by PhpStorm.
 * Author: Huan.liu
 * Date: 2020/1/10
 * Time: 16:07
 */
namespace App\Libs\Extra;


use App\Libs\GuzzleRequest;
use Hyperf\Di\Annotation\Inject;

class Luosimao
{
    #[Inject] protected GuzzleRequest $guzzleRequest;


    private string $message_send_json = 'http://sms-api.luosimao.com/v1/send.json';
    private string $message_send_bath_json = 'http://sms-api.luosimao.com/v1/send_batch';
    private string $message_send_xml= 'http://sms-api.luosimao.com/v1/send.xml';


    public function sendMessage($mobile , $message)
    {
        if ($message === '' || !is_numeric($mobile)) return false;
        $data = [
            'mobile' => $mobile,
            'message' => $message
        ];
        $options = [
            'curl' => [
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                CURLOPT_USERPWD  => 'api:key-'.config('luosimao')['key'],
            ],
        ];
        //        $result = self::curl(self::$apiList['message_send_bath_json'],$data);
        return $this->guzzleRequest->post($this->message_send_json,$data,$options);
    }


    public function sendMessageList($mobile_list , $message)
    {
        if ($message === '') return false;
        $data = [
            'mobile_list' => $mobile_list,
            'message' => $message
        ];
        $options = [
            'curl' => [
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                CURLOPT_USERPWD  => 'api:key-fe3292f1a8d0c374cf13a603bbd502d0',
            ],
        ];
//        $result = self::curl(self::$apiList['message_send_bath_json'],$data);
        return $this->guzzleRequest->post($this->message_send_bath_json,$data,$options);
    }









    //协程框架不可以使用curl阻塞程序
    static private function curl($url, $data = null,$type = 'post')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-fe3292f1a8d0c374cf13a603bbd502d0');

        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $res = curl_exec( $ch );
        curl_close( $ch );
        //$res  = curl_error( $ch );
//        var_dump($res);
        return $res;
    }
}
